package cn.ytr.project.service.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import cn.ytr.project.domain.Employee;
import cn.ytr.project.mapper.EmployeeMapper;
import cn.ytr.project.service.IEmployeeService;
import cn.ytr.project.util.MybatisUtil;

public class EmployeeServiceImpl implements IEmployeeService{

	@Override
	public List<Employee> selectAllForList() {
		SqlSession session = MybatisUtil.getSession();
		EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
		List<Employee> list = employeeMapper.selectAllForList();
		session.close();
		return list;
	}
	
	@Override
	public Employee selectOneById(Long id) {
		SqlSession session = MybatisUtil.getSession();
		EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
		Employee employee = employeeMapper.selectOneById(id);
		session.close();
		return employee;
	}

	@Override
	public void insert(Employee employee) {
		SqlSession session = MybatisUtil.getSession();
		EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
		employeeMapper.insert(employee);
		session.commit();
		session.close();
	}

	@Override
	public void update(Employee employee) {
		SqlSession session = MybatisUtil.getSession();
		EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
		employeeMapper.update(employee);
		session.commit();
		session.close();
	}

	@Override
	public void delete(Long id) {
		SqlSession session = MybatisUtil.getSession();
		EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
		employeeMapper.delete(id);
		session.commit();
		session.close();
	}

	@Override
	public List<Employee> selectForListByDeptId(Long deptId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Employee> selectForListByDeptIdIsNull() {
		SqlSession session = MybatisUtil.getSession();
		EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
		List<Employee> list = employeeMapper.selectForListByDeptIdIsNull();
		session.close();
		return list;
	}

	@Override
	public String[] getIdsByDeptId(Long deptId) {
		SqlSession session = MybatisUtil.getSession();
		EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
		String[] ids = employeeMapper.getIdsByDeptId(deptId);
		session.close();
		return ids;
	}
	
	@Test
	public void testSQL() throws Exception {
		String[] list = getIdsByDeptId(null);
		for (String id : list) {
			System.out.println(id);
		}
		updateDeptIdInIds(2L, list);
	}

	@Override
	public void updateDeptIdInIds(Long deptId, String[] ids) {
		SqlSession session = MybatisUtil.getSession();
		EmployeeMapper employeeMapper = session.getMapper(EmployeeMapper.class);
		employeeMapper.updateDeptIdInIds(deptId, ids);
		session.commit();
		session.close();
	}

}
